package ge.gen.eBdgt.action;

import ge.gen.eBdgt.service.DescrDaoService;
import ge.gen.eBdgt.service.DescrService;
import ge.gen.eBdgt.service.IcnDaoService;
import ge.gen.eBdgt.service.IcnService;
import ge.gen.eBdgt.vo.CmuTrackVO;
import ge.gen.eBdgt.vo.DescrVO;
import ge.gen.eBdgt.vo.IcnVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class BaseAction extends ActionSupport implements ServletRequestAware,
		ServletResponseAware {

	protected IcnService icnService;
	protected IcnVO icnVO;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected List<IcnVO> icnVOList;
	protected List<CmuTrackVO> cmuTrackVOList;
	protected HashMap<String, IcnVO> icnListMap;
	protected HashMap<String, CmuTrackVO> cmuTrackListMap;
	protected HashMap<String, String> descrMap;
	protected HashMap<String, String> statusMap;
	protected HashMap<String, String> ctgryMap;
	protected HashMap<String, String> reqEngMap;
	protected List<DescrVO> descrList;
	protected List<DescrVO> statusList;
	protected List<DescrVO> ctgryList;
	protected List<DescrVO> reqEngrList;
	protected List<String> icnList;
	protected static DescrService descrService;
	protected boolean addFlag = true;
	protected HttpSession session;
	 
	public BaseAction() throws Exception {
		icnService = new IcnDaoService();
		descrService = new DescrDaoService();
		icnListMap = new HashMap<String, IcnVO>();
		if (getIcnListMap() == null || getIcnListMap().size() <= 0) {
			icnVOList = icnService.getAllIcns();
			cmuTrackVOList = icnService.getAllCmuTrack();
			icnListMap = new HashMap<String, IcnVO>();
		}
		cmuTrackListMap = getCmuTrackMap(cmuTrackVOList);
		icnList = getIcnDescrListFromVOList(icnVOList);		
		prep();
		
	}

	private HashMap<String, CmuTrackVO> getCmuTrackMap(
			List<CmuTrackVO> cmuTrackVOList) {
		HashMap<String, CmuTrackVO> map = new HashMap<String, CmuTrackVO>();
		Iterator<CmuTrackVO> iter = cmuTrackVOList.iterator();
		while (iter.hasNext()) {
			CmuTrackVO vo = iter.next();
			if (vo != null && isNotNull(vo.getOrderNo())) {
				String s = vo.getOrderNo();
				map.put(s.trim(), vo);
			}
		}
		return map;
	}

	@Override
	public void setServletRequest(final HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(final HttpServletResponse response) {
		this.response = response;
	}

	private List<String> getIcnDescrListFromVOList(List<IcnVO> icnVOList) {
		if (icnVOList == null)
			return new ArrayList<String>();
		Iterator<IcnVO> iter = icnVOList.iterator();
		List<String> list = new ArrayList<String>();
		descrMap = new HashMap<String, String>();
		descrMap.put("", "                 ");
		list.add("                 ");
		while (iter.hasNext()) {
			IcnVO vo = iter.next();
			if (vo != null) {
				list.add(vo.getIcn());
				vo = updateCmuTrackInfo(vo);
				String s = strTrim(vo.getDescription());
				if (isNotNull(s))
					descrMap.put(vo.getDescription(), s);
				icnListMap.put(vo.getIcn(), vo);
			}
		}
		return list;
	}

	private IcnVO updateCmuTrackInfo(IcnVO inputVO) {
		String s = inputVO.getTimesNo();
		if (isNotNull(s)) {
			s = s.trim();
			CmuTrackVO vo = cmuTrackListMap.get(s);
			if (vo != null) {
				inputVO.setOrderInfo(vo);
				inputVO.setOrderInfoExist(true);
			} else
				inputVO.setOrderInfoExist(false);
		}
		return inputVO;
	}

	protected void prep() {
		descrList = descrService.getAllDescr();
		statusList = descrService.getAllStatus();
		ctgryList = descrService.getAllCtgry();
		reqEngrList = descrService.getAllReqEngr();
		
		Iterator<DescrVO> iter = descrList.iterator();
		descrMap = new HashMap<String, String>();
		descrMap.put("", "                 ");
		while (iter.hasNext()) {
			DescrVO vo = iter.next();
			if (vo != null) {
				descrMap.put(vo.getName(), vo.getName());
			}
		}
		
		iter = statusList.iterator();
		statusMap = new HashMap<String, String>();
		statusMap.put("", "                 ");
		while (iter.hasNext()) {
			DescrVO vo = iter.next();
			if (vo != null) {
				String cd = vo.getName().split("-")[0];
				statusMap.put(cd, vo.getName());
			}
		}
		
		iter = ctgryList.iterator();
		ctgryMap = new HashMap<String, String>();
		ctgryMap.put("", "                 ");
		while (iter.hasNext()) {
			DescrVO vo = iter.next();
			if (vo != null) {
				String cd = vo.getName().split("-")[0];
				cd = cd.trim();
				ctgryMap.put(cd, vo.getName());
			}
		}
		
		iter = reqEngrList.iterator();
		reqEngMap = new HashMap<String, String>();
		//reqEngMap.put("", "                 ");
		while (iter.hasNext()) {
			DescrVO vo = iter.next();
			if (vo != null) {
				String cd = vo.getName().split("-")[0];
				reqEngMap.put(cd, vo.getName());
			}
		}
		reqEngMap= getSortedMap(reqEngMap);
	}

	public HashMap getSortedMap(HashMap hmap) {
		if(hmap ==null || hmap.size() <=0 )
			return hmap;
		HashMap map = new LinkedHashMap();
		List mapKeys = new ArrayList(hmap.keySet());
		List mapValues = new ArrayList(hmap.values());
		hmap.clear();
		TreeSet sortedSet = new TreeSet(mapValues);
		Object[] sortedArray = sortedSet.toArray();
		int size = sortedArray.length;
		for (int i = 0; i < size; i++) {
			map.put(mapKeys.get(mapValues.indexOf(sortedArray[i])),
					sortedArray[i]);
		}
		return map;
	}

	private String strTrim(String description) {
		if (isNotNull(description) && description.length() > 30)
			return description.substring(0, 30) + "....";
		else
			return null;
	}

	public boolean isNull(String s) {
		return (s == null || s.trim().length() <= 0);
	}

	public boolean isNotNull(String s) {
		return (s != null && s.trim().length() > 0);
	}

	public IcnVO getIcnVO() {
		return icnVO;
	}

	public void setIcnVO(IcnVO icnVO) {
		this.icnVO = icnVO;
	}

	public List<IcnVO> getIcnVOList() {
		return icnVOList;
	}

	public void setIcnVOList(List<IcnVO> icnVOList) {
		this.icnVOList = icnVOList;
	}

	public String add() throws Exception {
		addFlag = true;
		return "input";
	}

	public boolean isBlank(String s) {
		return s == null || s.length() <= 0;
	}

	public HashMap<String, IcnVO> getIcnListMap() {
		if (icnListMap == null)
			icnListMap = (HashMap<String, IcnVO>) request.getSession()
					.getAttribute("icnListMap");
		return icnListMap;
	}

	public void setIcnListMap(HashMap<String, IcnVO> icnListMap) {
		this.icnListMap = icnListMap;
	}

	public HashMap<String, String> getDescrMap() {
		return descrMap;
	}

	public void setDescrMap(HashMap<String, String> descrMap) {
		this.descrMap = descrMap;
	}

	public List<String> getIcnList() {
		return icnList;
	}

	public void setIcnList(List<String> icnList) {
		this.icnList = icnList;
	}

	public boolean isAddFlag() {
		return addFlag;
	}

	public void setAddFlag(boolean addFlag) {
		this.addFlag = addFlag;
	}

	public HashMap<String, String> getStatusMap() {
		return statusMap;
	}

	public void setStatusMap(HashMap<String, String> statusMap) {
		this.statusMap = statusMap;
	}

	public HashMap<String, String> getCtgryMap() {
		return ctgryMap;
	}

	public void setCtgryMap(HashMap<String, String> ctgryMap) {
		this.ctgryMap = ctgryMap;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public HashMap<String, String> getReqEngMap() {
		return reqEngMap;
	}

	public void setReqEngMap(HashMap<String, String> reqEngMap) {
		this.reqEngMap = reqEngMap;
	}
}
